package expansion.sort;

import java.util.Arrays;

public class TestSort {
    public static void main(String[] args) {
        int[] arr = {7,3,6,9,2,5,4,0,8};
        shellSort(arr);
        System.out.println(Arrays.toString(arr));
    }
//    插入排序
    public static void insert(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int temp = arr[i];
            int j;
            for (j = i-1; j >= 0 && arr[j]>temp ; j--) {
                arr[j+1] = arr[j];
            }
            arr[j+1] = temp;
        }
    }

//    冒泡排序：相邻的两个比较
    private static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j]>arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

//    选择排序：找最小值
    private static void selectSort(int[] a){
        for (int i = 0; i < a.length-1; i++) {
            int min = i;
            for (int j = i+1; j < a.length; j++) {
                if (a[min]>a[j]){
                    min = j;
                }
            }
            if (min!=i){
                a[i] = a[min]^a[i];
                a[min] = a[min]^a[i];
                a[i] = a[min]^a[i];
            }
        }
    }

//    插入排序
    private static void insertSort(int[] a){
        for (int i = 1; i < a.length; i++) {
            int temp = a[i];
            int j;
            for (j = i-1; j >= 0; j--) {
                if (a[j]>temp){
                    a[j+1] = a[j];
                }else {
                    break;
                }
            }
            a[j+1] = temp;
        }
    }

//    希尔排序
    public static void shellSort(int[] arr){
        for (int gap = arr.length; gap > 0; gap/=2) {
            for (int i = gap; i < arr.length; i++) {//26547
                int temp = arr[i];
                int j;
                for (j = i-gap; j >= 0 ; j-=gap) {
                    if (arr[j]>temp){
                        arr[j+gap] = arr[j];
                    }else {
                        break;
                    }
                }
                arr[j+gap] = temp;
            }
        }
    }
}
